var app = getApp();
var myaudio = wx.createInnerAudioContext();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderlist:[],
    currentTabsIndex:1,
    now_page:1,
    totalpage:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getdoing(this.data.currentTabsIndex);
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     
  },
  // 获取进行中数据
  getdoing:function(type){
      var that = this;
      var userid = wx.getStorageSync("userId");
      var param = {
        url:"api/user/demandList",
        data:{
          user_id:userid,
          now_page:1,
          type:type
        },
        success:function(e){
          console.log(e)
          var order = e.data.data.list;
          that.setData({
            orderlist:order,
            totalpage:e.data.data.total_page
          })
        },
      };
      app.post(param);
  },
  // tab
  navTap: function (e) {
    if (this.data.currentTabsIndex == e.currentTarget.dataset.index) {
      return false;
    } else {
      this.setData({
        currentTabsIndex: e.currentTarget.dataset.index,
        now_page:1,
        totalpage:0
      })
      this.getdoing(e.currentTarget.dataset.index);
    }
  },

  onReachBottom: function () {
    var that = this;
    // 显示加载图标
   
    var now_page = parseInt(that.data.now_page)+1;
    // 页数+1
    if (now_page > parseInt(that.data.totalpage)) {
      wx.showToast({
        title: '没有更多数据了',
        icon: "none",
        duration: 2000
      })
      return false;
    }else{
      wx.showLoading({
        title: '玩命加载中',
      })
      var that = this;
      var userid = wx.getStorageSync("userId");
      var param = {
        url: "api/user/demandList",
        data: {
          user_id: userid,
          now_page: now_page,
          type: that.data.currentTabsIndex
        },
        success: function (e) {
          that.setData({
            orderlist: that.data.orderlist.concat(e.data.data.list),
            now_page: now_page
          })
          // 隐藏加载框
          wx.hideLoading();

        },
      };
      app.post(param);
    }
  

  },

  seedetail:function(e){
    var id = e.currentTarget.dataset.id;
    var status = e.currentTarget.dataset.status;
    if (parseInt(status) == 1 || parseInt(status) == 2){
        wx.switchTab({
          url: '/pages/index/index/index',
        })
    }else{
      wx.navigateTo({
        url: '/pages/mine/orderdetail/orderdetail?id='+id,
      })
    }
  },

  // 删除订单
  delit: function (e) {
    var that = this;
    wx.showModal({
      title: '提醒',
      content: '确定要删除此订单吗？',
      success: function (res) {
        if (res.confirm) {
          that.delorder(e)
        } else if (res.cancel) {
          return false;
        }
      }
    })
  },
  delorder: function (e) {
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    console.log(id);
    var that = this;
    var param = {
      url: 'api/user/delDemand',
      data: {
        demand_id: id
      },
      success: function (res) {
        console.log(res)
        that.data.orderlist.splice(index,1);
        that.setData({
          orderlist:that.data.orderlist
        })
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    };
    app.post(param);
  },

  
})